var config = require('../config');
var mongoose = require('mongoose');
var moment = require('moment');

//mongoose
var url = 'mongodb://' + config.host + '/' + config.db;
mongoose.connect(url, {
    user: config.user,
    pass: config.pwd
});

require('../models/user.js');
require('../models/category.js');
require('../models/store.js');
require('../models/question.js');
require('../models/paper.js');

var User = mongoose.model('User');
var Category = mongoose.model('Category');
var Store = mongoose.model('Store');
var Question = mongoose.model('Question');
var Paper = mongoose.model('Paper');

Store.find({}).exec(function (err, stores) {
    for (var i = 0; i < stores.length; i++) {
        Question.find({store: stores[i]._id}).limit(5).populate('store').exec(function (err, qs) {
            console.log(qs.length);
            if(qs.length > 0){
                var s = qs[0].store;
                console.log(s);
                for(var j = 0; j< 10; j++){
                    new Paper({
                        text: '测试试卷_' + j,
                        store: s._id,
                        totalScore : qs.length * 5,
                        questions: qs
                    }).save(function (err) {
                            if (err) throw err;
                        });
                }
            }
        });
    }
});




